using System;
using System.Collections.Generic;
using System.Reflection;

namespace TestUtils
{
    public class DefinitionExplorer<T> where T : TestUtils.DefinitionFixture, new()
    {
        private int count = -1;
        private IDictionary<string, IList<string>> definition;
        private List<FieldInfo> fields = null;

        public DefinitionExplorer()
        {
            Type type = typeof(T);
            if (ContextManager.TestContext.FixedDefinitions.TryGetValue(type.Name, out definition))
            {
                SetFields(type);
                if (fields.Count >= 0)
                {
                    count = 0;
                }
            }
        }

        private void SetFields(Type type)
        {
            fields = new List<FieldInfo>();
            FieldInfo[] fieldInfos = type.GetFields();
            foreach (FieldInfo field in fieldInfos)
            {
                if (field.FieldType.Equals(typeof(string)) && (field.IsPublic))
                {
                    fields.Add(field);
                }
            }
        }


        public int Count
        {
            get { return count == 0 ? definition[fields[0].Name].Count : 0; }
        }

        public T this[int index]
        {
            get
            {
                T result = new T();
                foreach (FieldInfo fieldInfo in fields)
                {
                    fieldInfo.SetValue(result, definition[fieldInfo.Name][index]);
                }
                return result;
            }
        }

     
    }
}